﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NETXPF.Library
{
    public class SortUtils
    {
        public class PropertyComparer<T> : IComparer<T>, System.Collections.IComparer
        {
            public string PropertyName { get; set; }
            public object[] PropertyIndexers { get; set; }
            public bool Reversed { get; set; }
            public PropertyComparer()
            {
                this.PropertyName = null;
                this.PropertyIndexers = null;
                this.Reversed = false;
            }
            public PropertyComparer(string propertyName)
                : this()
            {
                this.PropertyName = propertyName;
            }
            public int Compare(T x, T y)
            {
                if (Reversed)
                {
                    T z = x;
                    x = y;
                    y = z;
                }
                int compareResult = ((IComparable)x.GetType().GetProperty(PropertyName).GetValue(x, this.PropertyIndexers)).CompareTo(y.GetType().GetProperty(PropertyName).GetValue(y, this.PropertyIndexers));
                return compareResult;
            }

            #region IComparer Members

            public int Compare(object x, object y)
            {
                if (Reversed)
                {
                    object z = x;
                    x = y;
                    y = z;
                }
                int compareResult = ((IComparable)x.GetType().GetProperty(PropertyName).GetValue(x, this.PropertyIndexers)).CompareTo(y.GetType().GetProperty(PropertyName).GetValue(y, this.PropertyIndexers));
                return compareResult;
            }

            #endregion
        }

        public static T[] SortedArray<T>(ICollection<T> collection, IComparer<T> comparer)
        {
            T[] array = new T[collection.Count];
            collection.CopyTo(array, 0);
            if (comparer != null)
            {
                Array.Sort(array, comparer);
            }
            return array;
        }

        public static T[] SortedArray<T>(ICollection<T> collection, IComparer<T> comparer, string[] filterCols, object[] filterValues)
        {
            //filter
            ICollection<T> nuCollection = new List<T>();
            foreach (T t in collection)
            {
                int i = 0;
                bool match = true;
                foreach (string name in filterCols)
                {
                    if (!t.GetType().GetProperty(name).GetValue(t, null).Equals(filterValues[i]))
                    {
                        match = false;
                        break;
                    }
                    i++;
                }
                if (match)
                {
                    nuCollection.Add(t);
                }
            }
            return SortedArray<T>(nuCollection, comparer);
        }
    }
}
